/*
 * AbstractEntity.java
 * 
 * Created on Nov 18, 2007, 12:29:21 AM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.usixml.entity.util;

import java.util.UUID;

/**
 *
 * @author htmfilho
 */
public class AbstractEntity implements Entity {
protected String id;
    
    public String getId() {
        if(this.id == null)
            this.id = createId();
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    @Override
    public boolean equals(Object object) {
        if(this == object) return true;
        if(object == null || !(object instanceof Entity)) return false;
        
        Entity other = (Entity)object;
        
        if(id == null)
            return false;
        
        return id.equals(other.getId());
    }
    
    @Override
    public int hashCode() {
        if(id != null)
            return id.hashCode();
        else
            return super.hashCode();
    }
    
    private static String createId() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "");
    }
}